package scottb.cribbage.model {
import mx.collections.ArrayCollection;

[Bindable]
public class Player {

    private var originalName:String;
    public var name:String;
    public var hands:ArrayCollection = new ArrayCollection();
    public var hand:Hand;
    public var score:int;

    public function Player(n:String) {
        originalName = n;
        name = n;
    }

    public function resetName():void {
        name = originalName;
    }

    public function addPeggingPoints(amount:int):void {
        if (hand.peggingPoints.points + amount >= 0) {
            hand.peggingPoints.points += amount;
            updateScore();
        }
    }

    public function addCountingPoints(amount:int):void {
        if (hand.countingPoints.points + amount >= 0) {
            hand.countingPoints.points += amount;
            updateScore();
        }
    }

    public function addCribPoints(amount:int):void {
        if (hand.cribPoints.points + amount >= 0) {
            hand.cribPoints.points += amount;
            updateScore();
        }
    }

    public function newHand():void {
        
        hand = new Hand(false);
        hands.addItem(hand);
    }

    public function newCribHand():void {

        hand = new Hand(true);
        hands.addItem(hand);
    }

    private function updateScore():void {

        var tempScore:int = 0;

        for each(var hand:Hand in hands) {
            tempScore += hand.points.points;
        }

        score = tempScore;
    }
}
}
